package com.xuxingcan.findmyvoice.Activity;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;

import com.rey.material.app.DialogFragment;
import com.rey.material.widget.EditText;
import com.rey.material.widget.ProgressView;
import com.xuxingcan.findmyvoice.Model.FeedbackModel;
import com.xuxingcan.findmyvoice.R;
import com.xuxingcan.findmyvoice.Util.LeanCloudUtil;

/**
 * Created by xuxingcan on 16-4-6.
 */
public class FeedbackActivity extends BaseActivity {
    private AlertDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feed_back);
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        findViewById(R.id.send).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String title = ((EditText) findViewById(R.id.feed_back_title)).getText().toString();
                String content = ((EditText) findViewById(R.id.feed_back_content)).getText().toString();
                if (title == null || title.length() == 0) {
                    showDialog("发生了一点错误", "标题不能为空", "我知道了");
                    return;
                }
                if (content == null || content.length() == 0) {
                    showDialog("发生了一点错误", "内容不能为空", "我知道了");
                    return;
                }
                showLoadingProgress();
                final long startTime = System.currentTimeMillis();
                FeedbackModel model = new FeedbackModel();
                model.setTitle(title);
                model.setContent(content);
                LeanCloudUtil.sendFeedBack(model, new LeanCloudUtil.sendMsgCallBack() {
                    @Override
                    public void onError(Exception e) {

                    }

                    @Override
                    public void onSuccess() {
                        if (System.currentTimeMillis() - startTime < 2000) {
                            long time = 2000 + startTime - System.currentTimeMillis();
                            findViewById(R.id.send).postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    showDialog("谢谢", "感谢您的支持", "我知道了", null, new DialogCallback() {
                                        @Override
                                        public boolean onPositiveActionClicked(DialogFragment fragment) {
                                            finish();
                                            return false;
                                        }

                                        @Override
                                        public boolean onNegativeActionClicked(DialogFragment fragment) {
                                            return false;
                                        }
                                    });
                                }
                            }, time > 0 ? time : 0);
                        } else {
                            showDialog("谢谢", "感谢您的支持", "我知道了", null, new DialogCallback() {
                                @Override
                                public boolean onPositiveActionClicked(DialogFragment fragment) {
                                    finish();
                                    return false;
                                }

                                @Override
                                public boolean onNegativeActionClicked(DialogFragment fragment) {
                                    return false;
                                }
                            });
                        }
                    }
                });
            }
        });
    }

    private void showLoadingProgress() {
        progressDialog = new AlertDialog.Builder(this).create();
        progressDialog.show();
        progressDialog.setCancelable(false);
        progressDialog.getWindow().setContentView(R.layout.progress_dialog);
        progressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        ((ProgressView) progressDialog.findViewById(R.id.progress)).start();
    }

    private void stopLoadingProgress() {
        if (progressDialog != null) {
            ProgressView mProgressView = (ProgressView) progressDialog.findViewById(R.id.progress);
            mProgressView.stop();
            mProgressView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    progressDialog.dismiss();
                }
            }, 200);
        }
    }
}
